package com.franzliszt.OrderOnline.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.franzliszt.OrderOnline.SQLite.Dao;
import com.franzliszt.OrderOnline.adapter.goodsListAdapter;
import com.franzliszt.OrderOnline.tool.GoodsList;
import com.franzliszt.OrderOnline.util.KillProcess;
import com.franzliszt.OrderOnline.util.SP;
import com.franzliszt.hospitalnumberingsystem.R;




import java.util.ArrayList;
import java.util.List;

public class GoodsListActivity extends AppCompatActivity {
    private RecyclerView GoodsListRecyclerView;
    private com.franzliszt.OrderOnline.adapter.goodsListAdapter goodsListAdapter;
    private List<GoodsList> goodsLists = new ArrayList<>(  );
    private List<GoodsList> dataList = new ArrayList<>(  );
    private GoodsList goodsList;
    private Dao dao;
    private Context context = null;
    private double totalPrice =0;
    private TextView goodsTotalNumber,goodsTotalPrice,other;
    private Button goodsAllClear;
    private KillProcess killProcess;
    private SP sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE );
            getWindow().setStatusBarColor( Color.TRANSPARENT );
        }
        setContentView( R.layout.activity_goods_list );
        InitView();
        InitRecycler();
        InitData();
        Listener();
        InitBarData();
        goodsAllClear.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodsListAdapter.removeAllItem( dataList );
                goodsTotalNumber.setText( 0+"" );
                goodsTotalPrice.setText( 0+"" );
            }
        } );
    }
    private void InitView(){
        GoodsListRecyclerView = findViewById( R.id.GoodsListRecyclerView );
        goodsTotalNumber = findViewById( R.id.goodsTotalNumber );
        goodsTotalPrice = findViewById( R.id.goodsTotalPrice );
        goodsAllClear = findViewById( R.id.clearAllGoods );
        other = findViewById(R.id.exchangeFunc);
        other.setVisibility(View.GONE);
        if (context == null){
            context = GoodsListActivity.this;
        }
        dao = new Dao( context );
        killProcess = KillProcess.getInstance();
        killProcess.addActivity( GoodsListActivity.this );
        sp = SP.getInstance();
    }
    private void InitRecycler(){
        LinearLayoutManager manager = new LinearLayoutManager( getApplicationContext() );
        GoodsListRecyclerView.setLayoutManager( manager );
        goodsListAdapter = new goodsListAdapter( context,R.layout.goods_item,dataList );
        GoodsListRecyclerView.setAdapter( goodsListAdapter );
    }
    private void Listener(){
        goodsListAdapter.setOnItemChildClickListener( new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                int num = dataList.get( position ).getNumber();
                double price = dataList.get( position ).getPrice();
                switch (view.getId()){
                    case R.id.goodsAdd:
                        goodsListAdapter.addPrice(dataList,price,position,num);
                        InitBarData();
                        //Toast.makeText( GoodsListActivity.this,"Add"+num,Toast.LENGTH_SHORT ).show();
                        break;
                    case R.id.goodsSub:
                        goodsListAdapter.subPrice(dataList,price,position,num);
                        InitBarData();
                        //Toast.makeText( GoodsListActivity.this,"Sub"+num,Toast.LENGTH_SHORT ).show();
                        break;
                }
            }
        } );
    }
    private void InitData(){
        goodsLists = dao.QueryAll();
        for (int i = 0; i <goodsLists.size() ; i++) {
            goodsList = new GoodsList(goodsLists.get(i).getUserName(),goodsLists.get(i).getPassWord(), goodsLists.get( i ).getImg(),goodsLists.get( i ).getName(),goodsLists.get( i ).getPrice(),goodsLists.get( i ).getNumber(),goodsLists.get( i ).getSub(),goodsLists.get( i ).getAdd() );
            dataList.add( goodsList );
        }
    }
    private void InitBarData(){
        goodsTotalNumber.setText( goodsListAdapter.TotalNumber( dataList )+"" );
        goodsTotalPrice.setText(FunctionActivity.doubleToString( goodsListAdapter.TotalPrice( dataList ) ) );
    }
    public void Exit(View view){
        ReturnActivity(FunctionActivity.class);
    }

    private void ReturnActivity(Class Activity){
        startActivity( new Intent( context,Activity ) );
    }

    public void ReturnFirstPage(View view) {
        ReturnActivity(FunctionActivity.class);
    }

    public void ReturnPayment(View view) {
        String monry = goodsTotalPrice.getText().toString();
        sp.PutData(GoodsListActivity.this,"TotalPrice",monry);
        ReturnActivity(PaymentActivity.class);

    }
}
